import requests
import time

class Sender:

    def __init__(self, authorization, channelid, application_id, guild_id, session_id, version, id, flags, proxy):
        self.authorization = authorization
        self.channelid = channelid
        self.application_id = application_id
        self.guild_id = guild_id
        self.session_id = session_id
        self.version = version
        self.id = id
        self.flags = flags
        self.headers = {
            'authorization': self.authorization
        }
        self.proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else {}


    def send(self, prompt):
        # prompt = prompt.replace('_', ' ')
        # prompt = " ".join(prompt.split())
        # prompt = re.sub(r'[^a-zA-Z0-9\s]+', '', prompt)
        # prompt = prompt.lower()

        payload = {'type': 2,
                   'application_id': self.application_id,
                   'guild_id': self.guild_id,
                   'channel_id': self.channelid,
                   'session_id': self.session_id,
                   'data': {
                       'version': self.version,
                       'id': self.id,
                       'name': 'imagine',
                       'type': 1,
                       'options': [{'type': 3, 'name': 'prompt', 'value': str(prompt) + ' ' + self.flags}],
                       'attachments': []}
                   }

        r = requests.post(
            'https://discord.com/api/v9/interactions',
            json=payload,
            headers=self.headers,
            proxies=self.proxies)

        # while r.status_code != 204:
        #     time.sleep(5)
        #     r = requests.post(
        #         'https://discord.com/api/v9/interactions',
        #         json=payload,
        #         headers=self.headers,
        #         proxies=self.proxies
        #     )
        print(r.status_code)
        print(r)

        print(r.text)
        print('prompt [{}] successfully sent!'.format(prompt))

    def send_UVX(self, message_id, component_type, custom_id):

        payload = {
            'type': 3,
            'application_id': self.application_id,
            'guild_id': self.guild_id,
            'channel_id': self.channelid,
            'session_id': self.session_id,
            'message_id': message_id,
            'data': {
               'component_type': component_type,
               'custom_id': custom_id
            }
        }

        r = requests.post(
            'https://discord.com/api/v9/interactions',
            json=payload,
            headers=self.headers,
            proxies=self.proxies)

        # while r.status_code != 204:
        #     time.sleep(5)
        #     r = requests.post(
        #         'https://discord.com/api/v9/interactions',
        #         json=payload,
        #         headers=self.headers,
        #         proxies=self.proxies
        #     )
        print(r.text)
